import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

class OneLineInputWidget extends StatefulWidget {
  const OneLineInputWidget({
    super.key,
    this.placeholder,
    this.placeholderStyle,
    this.style,
    this.controller,
    this.onSubmitted,
    this.textInputAction,
    this.keyboardType,
    this.textAlign = TextAlign.start,
    this.enabled = true,
    this.obscureText = false,
    this.width,
    this.radius = 0,
    this.margin,
    this.padding,
    this.bgColor,
    this.height,
    this.focusNode,
    this.showCursor = false,
    this.defaultText,
    this.onChanged,
    this.inputFormatters,
  });

  final String? placeholder;
  final TextStyle? placeholderStyle;
  final TextStyle? style;
  final TextEditingController? controller;
  final ValueChanged<String>? onSubmitted;
  final ValueChanged<String>? onChanged;
  final TextInputAction? textInputAction;
  final TextInputType? keyboardType;
  final TextAlign textAlign;
  final bool enabled;
  final bool obscureText;
  final double? width;
  final double radius;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final Color? bgColor;
  final double? height;
  final FocusNode? focusNode;
  final bool showCursor;
  final String? defaultText;
  final List<TextInputFormatter>? inputFormatters;

  @override
  State<OneLineInputWidget> createState() => _OneLineInputWidgetState();
}

class _OneLineInputWidgetState extends State<OneLineInputWidget> {
  late FocusNode actualFocusNode;
  late TextEditingController actualController;
  bool isFocused = false;

  @override
  void initState() {
    super.initState();

    actualFocusNode = widget.focusNode ?? FocusNode();
    actualController = widget.controller ?? TextEditingController();

    if (widget.showCursor) {
      Future.delayed(Duration.zero, () {
        FocusScope.of(Get.context!).requestFocus(actualFocusNode);
      });
    }
    if ((widget.defaultText != null) && (widget.defaultText!.isNotEmpty)) {
      actualController.text = widget.defaultText!;
    }

    /// 监听焦点变化
    actualFocusNode.addListener(() {
      if (actualFocusNode.hasFocus) {
        isFocused = true;
      } else {
        isFocused = false;
      }
      setState(() {});
    });
  }

  @override
  void dispose() {
    if (widget.controller == null) {
      actualController.dispose();
    }
    if (widget.focusNode == null) {
      actualFocusNode.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool showSuffix = widget.enabled && isFocused;
    return Container(
      width: widget.width ?? 200.r,
      height: widget.height ?? 40.r,
      margin: widget.margin,
      padding: widget.padding,
      decoration: BoxDecoration(
        color: widget.bgColor,
        borderRadius: BorderRadius.circular(widget.radius),
      ),
      child: TextField(
        controller: actualController,
        decoration: InputDecoration(
          hintText: widget.placeholder ?? "please_input".tr,
          hintStyle: widget.placeholderStyle ?? StyleUtils.ts_99_12_400,
          border: InputBorder.none,
          suffixIcon: showSuffix
              ? GestureDetector(
                  onTap: () {
                    actualController.clear();
                  },
                  child: Icon(
                    Icons.clear_outlined,
                    color: ColorUtils.c_9a9a9a,
                  ),
                )
              : null,
        ),
        style: widget.style ?? StyleUtils.ts_33_13_500,
        //键盘回车搜索
        textInputAction: widget.textInputAction,
        onSubmitted: widget.onSubmitted,
        onChanged: widget.onChanged,
        keyboardType: widget.keyboardType,
        textAlign: widget.textAlign,
        enabled: widget.enabled,
        obscureText: widget.obscureText,
        focusNode: actualFocusNode,
        inputFormatters: widget.inputFormatters,
      ),
    );
  }
}
